﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.MTHHelper;
using 扫码枪应用项目;

namespace MYZNMarking
{
    public partial class FrmData : Form
    {
        public FrmData()
        {
            InitializeComponent();
            this.dgv_Data.AutoGenerateColumns = false;
            this.dgv_Data.Columns[0].DefaultCellStyle.Format = "yyyy-MM-dd  HH:mm:ss";
            //当前时间
            this.dtp_Start.Value = DateTime.Now.AddHours(-2.0);
            this.dtp_End.Value = DateTime.Now;
        }
        private void FormData_Load(object sender, EventArgs e)
        {
            dgv_Data.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
        private void dgv_Data_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint(this.dgv_Data, e);
        }

        private HistoryDataService historyService = new HistoryDataService();
        private List<HistoryData> historyDatas = new List<HistoryData>();
        private void btn_Inquire_Click(object sender, EventArgs e)
        {
            TimeSpan timeSpan = this.dtp_End.Value - this.dtp_Start.Value;
            if (timeSpan.TotalSeconds < 0)
            {
                MessageBox.Show("开始时间不能大于结束时间");
                return;
            }
            var result = historyService.GetHistoryDataByTime(this.dtp_Start.Value, this.dtp_End.Value);
            if (result.IsSuccess)
            {
                if (result.Content.Count > 0)
                {
                    this.historyDatas = result.Content;
                    this.dgv_Data.DataSource = null;
                    this.dgv_Data.DataSource = result.Content;
                }
                else
                {
                    MessageBox.Show("该时间段未查询到数据");

                }

            }
            else
            {
                MessageBox.Show("查询出错");
            }
        }



        private void btn_Export_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //标题
            saveFileDialog.Title = "请选择文件";
            saveFileDialog.Filter = "Excel文件（*.xlsx）|*.xlsx|CSV文件（*.csv）|*.csv";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.FileName = "数据记录" + this.dtp_Start.Value.ToString("yyyyMMddHHmmss") + "_" + this.dtp_End.Value.ToString("yyyyMMddHHmmss");
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    MiniExcel.SaveAs(saveFileDialog.FileName, historyDatas);
                    Process.Start(saveFileDialog.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出文件失败" + ex.Message);
                }
            }
        }

 
    }

}

